﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ActivitiesControl.Domain.DAL;
using ActivitiesControl.Domain.Utils;

namespace ActivitiesControl.Domain.Model
{
	internal class ActivityHandler
	{
		private IActivityRepository repository;
		
		internal ActivityHandler(IActivityRepository repository)
		{
			this.repository = repository;
		}
		
		public void Register(
			string description,
			string activityType,
			int effortHours,
			Date date,
			int projectId,
			int resourceId)
		{
			Activity activity = new Activity
			{
				Date = date,
				Description = description,
				EffortHours = effortHours,
				ActivityType = activityType,
				ProjectId = projectId,
				ResourceId = resourceId
			};

			ValidateActivity(activity);

			repository.Add(activity);
		}

		public void Update(
			int activityId,
			string description,
			string activityType,
			int effortHours,
			Date date,
			int projectId,
			int resourceId)
		{
			Activity activity = new Activity
			{
				Id = activityId,
				Date = date,
				Description = description,
				EffortHours = effortHours,
				ActivityType = activityType,
				ProjectId = projectId,
				ResourceId = resourceId
			};

			ValidateActivity(activity);

			repository.Update(activity);
		}

		public void Exclude(int activityId)
		{
			repository.RemoveById(activityId);
		}

		private Activity ValidateActivity(Activity activity)
		{
			if (activity.Date.DateTime > DateTime.Now)
				throw new DomainException("Não é possível registrar uma atividade no futuro.");

			if (activity.EffortHours < 1 || activity.EffortHours > 23)
				throw new DomainException("As horas não podem ser maiores do que 23 ou menores do que 1.");

			if (string.IsNullOrEmpty(activity.Description))
				throw new DomainException("A descrição é obrigatória");

			return activity;
		}
	}
}
